//
//  ChooseView.m
//  LClass
//
//  Created by 严军 on 16/5/30.
//  Copyright © 2016年 YanJun. All rights reserved.
//

#import "ChooseView.h"
#import "ChooseTF.h"
@implementation ChooseView
{
    UIButton* btnSelected;//选中按钮
    UIButton* btnChooseClass;//选择班级按钮
    int courseId;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(instancetype)init
{
    self=[super init];
    if (self) {
        
        [self layout];
        
    }
    
    return self;
}

-(void)layout
{
    self.backgroundColor=ThemeBackgroundColor;
    
    //请选择班级
    UILabel* labChooseClass=[[UILabel alloc]init];
    [self addSubview:labChooseClass];
    [labChooseClass mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.left.mas_equalTo(16);
    }];
    labChooseClass.text=@"请选择课程：";
    labChooseClass.textColor=[UIColor darkGrayColor];
    labChooseClass.font=[UIFont systemFontOfSize:16];
    
    //按钮
     btnChooseClass=[[UIButton alloc]init];
    [self addSubview:btnChooseClass];
    [btnChooseClass mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labChooseClass.mas_bottom).offset(20);
        make.width.equalTo(self).multipliedBy(0.7);
        make.height.mas_equalTo(35);
        make.centerX.equalTo(self);
    }];
    [[Utility sharedInstance] setLayerView:btnChooseClass borderW:1 borderColor:[UIColor lightGrayColor] radius:8];
    btnChooseClass.backgroundColor=ThemeBackgroundColor;
    [btnChooseClass setTitle:@"" forState:0];
    btnChooseClass.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btnChooseClass setTitleColor:TitleColorLight forState:0];
    //添加小图标
    UIImageView* imgV=[[UIImageView alloc]init];
    [btnChooseClass addSubview:imgV];
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(btnChooseClass);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(35);
    }];
    imgV.image=[UIImage imageNamed:@"choose"];
    [btnChooseClass addTarget:self action:@selector(clickToPopView:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //请选择考勤方式
    UILabel* labChooseType=[[UILabel alloc]init];
    [self addSubview:labChooseType];
    [labChooseType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btnChooseClass.mas_bottom).offset(20);
        make.left.equalTo(labChooseClass);
    }];
    labChooseType.text=@"请选择考勤方式：";
    labChooseType.textColor=[UIColor darkGrayColor];
    labChooseType.font=[UIFont systemFontOfSize:16];
    
    //三个考勤按钮
    UIButton* lastBtn;
    for(int i=0;i<4;i++)
    {
        UIButton* btn=[[UIButton alloc]init];
        btn.backgroundColor=ThemeBackgroundColor;
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self addSubview:btn];
        if (lastBtn) {
            [[Utility sharedInstance] setLayerView:btn borderW:1 borderColor:[UIColor lightGrayColor] radius:8];
            [btn setTitleColor:[UIColor lightGrayColor] forState:0];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.left.equalTo(lastBtn);
                make.top.equalTo(lastBtn.mas_bottom).offset(20);
            }];
            if(i==1)
            {
                [btn setTitle:@"全员考勤" forState:0];
            }else if(i==2)
            {
                [btn setTitle:@"1/3考勤" forState:0];
            }else if(i==3)
            {
                [btn setTitle:@"1/2考勤" forState:0];
            }
        }else
        {
            [[Utility sharedInstance] setLayerView:btn borderW:1 borderColor:TitleColorLight radius:8];
            [btn setTitleColor:TitleColorLight forState:0];
             btn.selected=YES;
             btnSelected=btn;
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.left.equalTo(btnChooseClass);
                make.height.equalTo(self).multipliedBy(0.1);
                make.top.equalTo(labChooseType.mas_bottom).offset(20);
            }];
            [btn setTitle:@"随机考勤" forState:0];
        }
        btn.tag=i+1;
        [btn addTarget:self action:@selector(choolsedType:) forControlEvents:UIControlEventTouchUpInside];
        lastBtn=btn;
        
    }
}

-(void)choolsedType:(UIButton* )btn
{
    //1.取消btnSelected状态
    btnSelected.selected=NO;
    [[Utility sharedInstance] setLayerView:btnSelected borderW:1 borderColor:[UIColor lightGrayColor]  radius:8];
    [btnSelected setTitleColor:[UIColor lightGrayColor] forState:0];
    
    //2.设置选中状态
    [[Utility sharedInstance] setLayerView:btn borderW:1 borderColor:TitleColorLight radius:8];
    [btn setTitleColor:TitleColorLight forState:0];
    btn.selected=YES;
    //3.赋值选中状态
    btnSelected=btn;
    
    NSDictionary* parDic=@{@"courseId":[NSString stringWithFormat:@"%d",courseId],@"chooseType":[NSString stringWithFormat:@"%ld",(long)btn.tag]};
    _parmBlock(parDic);
}

//选择课程
-(void)clickToPopView:(UIButton*)sender
{
    ChooseTF* Tview=[[ChooseTF alloc]init];
    Tview.chooseBlock=^(NSDictionary* dicParam){
        //选中后更新界面
        [btnChooseClass setTitle:[dicParam objectForKey:@"courseName"] forState:0];
        courseId=[(NSNumber*)[dicParam objectForKey:@"courseId"] intValue];
    };
    Tview.dataList=_dataList;
   // Tview.dataList=@[@{@"className":@"计算机科学"},@{@"className":@"经济学管理应用"},@{@"className":@"社会实践应用"},@{@"className":@"高等数学"},@{@"className":@"操作系统"}];
    
    if ([[Utility sharedInstance] arryIsNotNull:Tview.dataList]) {
        
        //弹出tableview以供选择
        [Tview popView];
    }else
    {
        
    }
}

@end
